<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-6-25
 * Time: 上午11:38
 */

namespace common\components\gps;


use common\components\tools\Curl;
use yii\base\ErrorException;
use yii\httpclient\Client;

class BeiDou extends Gps implements Api
{
    const PRE_SIGN = '863';
    const NOTE = '北斗'; // http://fk.gpsonline.cn/fw/getView?fwCoord.project=&fwCoord.function=login&fwCoord.view=login

    public function addGps()
    {
        $this->checkOrder();
        $order = $this->order;
        $_data = unserialize($order->data);
        /*$_x = [
            'client_name' => 'test2',
            'vehicleNum' => '浙DX911U',
        ];
        echo serialize($_x);exit;*/
        if (!isset($_data['client_name'])){
            throw new ErrorException("子组织名必填", 1024);
        }
        if (!isset($_data['vehicleNum'])){
//            throw new ErrorException("车牌号必填", 1024);
            $_data['vehicleNum'] = $_data['custom_name'];
        }
        if (!$_data['custom_name']){
            throw new ErrorException("没有找到客户名", 1026);
        }
        $req_data = [
            'params.acct' => Config::$bei_dou['acct'],
            'params.pwd' => md5(Config::$bei_dou['pwd']),
            'params.mechineNo' => $order->gps_serial_number,
            'params.userAcct' => $_data['client_name'],
//            'params.userAcct' => 'test',
            'params.userName' => $_data['client_name'],
//            'params.userName' => 'test',
            'params.vehicleNum' => $_data['vehicleNum'], // 车牌号
//            'params.vehicleNum' => '浙DX912U', // 车牌号
//            'params.frameId' => $order->gps_serial_number, // 车架号
//            'params.engineId' => $order->gps_serial_number, // 发动机号
//            'params.color' => $order->gps_serial_number, // 车辆颜色
//            'params.brand' => $order->gps_serial_number, // 车辆品牌（同步车辆信息使用）
            'params.loanerName' => $_data['custom_name'], // 客户姓名
//            'params.loanerContact' => $order->gps_serial_number, // 客户手机号
//            'params.connectNo' => $order->gps_serial_number, // 关联设备号
        ];
        if (isset($_data['frameId'])){
            $req_data['params.frameId'] = $_data['frameId'];
        }
        if (isset($_data['engineId'])){
            $req_data['params.engineId'] = $_data['engineId'];
        }
        if (isset($_data['color'])){
            $req_data['params.color'] = $_data['color'];
        }
        if (isset($_data['brand'])){
            $req_data['params.brand'] = $_data['brand'];
        }
        if (isset($_data['loanerName'])){
            $req_data['params.loanerName'] = $_data['loanerName'];
        }
        if (isset($_data['loanerContact'])){
            $req_data['params.loanerContact'] = $_data['loanerContact'];
        }
        if (isset($_data['connectNo'])){
            $req_data['params.connectNo'] = $_data['connectNo'];
        }
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl(Config::$bei_dou['sync_url'])
            ->setData($req_data)
            ->send();
        $o = json_decode($response->content, true, 512);
        if ($o['success']&&$o['errorMsg'] == "设备车辆同步信息成功！"){}else{
            throw new ErrorException("北斗sync失败:".$response->content);
        }
    }

    public function getGps()
    {
        $this->checkOrder();
        $order = $this->order;
        $get_data = [
            'params.acct' => Config::$bei_dou['acct'],
            'params.pwd' => md5(Config::$bei_dou['pwd']),
            'params.mechineNo' => $order->gps_serial_number,
        ];
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl(Config::$bei_dou['get_gps_url'])
            ->setData($get_data)
            ->send();
        $other_data['gps_status'] = '未上线';
        if (isset($response->content)){
            $x = json_decode($response->content, true, 512);
            if (isset($x['resultData']['status'])){
                if (isset($x['resultData']['time'])){
                    $t = substr($x['resultData']['time'], 0, 10);
                    $abs = abs($t-time());
                    if ('141' == substr($order->gps_serial_number, 0, 3)){
                        $__t = 24*3600;
                    }else{
                        $__t = 600;
                    }
                    if ($abs < $__t){
                        $other_data['gps_status'] = $x['resultData']['status'];
                    }
                }
            }
        }
        return [
            'platform_back' => $response->content,
            'order' => $order->toArray(),
            'other_data' => $other_data,
        ];
    }

    public function getGpsLocationData()
    {
        $this->checkOrder();
        $order = $this->order;
        // http://fk.gpsonline.cn/inf/lrc/monitorMap?params.acct=易商金融&params.pwd=25d687e2c7e715b2f1c9bc30a47b0863&params.mechineNo=14143523800
//        $data['url'] = "http://fk.gpsonline.cn/inf/lrc/monitorMap?params.acct=易商金融&params.pwd=25d687e2c7e715b2f1c9bc30a47b0863&params.mechineNo=14143523800";
//        $data['url'] = Config::$bei_dou['get_gps_location_url']."?params.acct=".Config::$bei_dou['acct']."&params.pwd=".md5(Config::$bei_dou['pwd'])."&params.mechineNo=".$order->gps_serial_number;
        $data['url'] = Config::$bei_dou['get_gps_location_url']."?params.acct=".$order->gps_serial_number."&params.pwd=".md5(Config::$bei_dou['pwd'])."&params.mechineNo=".$order->gps_serial_number;
        $data['html'] = '<script type="text/javascript">location.href = "'.$data['url'].'";</script>';
        return $data;
    }

    public function getGpsLocationDataOld()
    {
        $this->checkOrder();
        $order = $this->order;
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl(Config::$bei_dou['get_gps_location_url'])
            ->setData([
                'params.acct' => Config::$bei_dou['acct'],
                'params.pwd' => md5(Config::$bei_dou['pwd']),
                'params.mechineNo' => $order->gps_serial_number,
            ])
            ->send();
        $o = json_decode($response->content, true, 512);
        $a = [];
        if (isset($o['success'])&&$o['success'] == true){
            foreach ($o['data'] as $k => $v) {
                $a['jingdu'] = $v['LNG'];
                $a['weidu'] = $v['LAT'];
            }
        }
        $data = ['other'=>$o];
        $data['url'] = self::getMapLocationByGaoDe($a['jingdu'], $a['weidu']);
        $data['html'] = '<script type="text/javascript">location.href = "'.$data['url'].'";</script>';
        return $data;
    }
}